package com.lemon.ai.socket.packets.spiritgame.model.generator;

import java.util.List;

import com.lemon.ai.socket.packets.spiritgame.model.db.MetaData;
import com.lemon.ai.socket.packets.spiritgame.model.dto.Table;
import com.lemon.ai.socket.packets.spiritgame.model.generator.utils.StringUtils;

public class FactoryGenerator extends AbstractGenerator {

	private String GET_METHODS = 
			"\r" + 
			"	public CLASS_NAMEDAO getCLASS_NAMEDAO() {\r" +
			"		return new CLASS_NAMEDAOImpl();\r" +
			"	}\r";
	
	public FactoryGenerator() {
		super("FactoryTemp.txt");
	}

	public String generate(List<Table> tables) {
		return super.temp.
					replace("PACKAGE_NAME", super.prop.getProperty("package")).
					replace("GET_METHODS", generateDaoGetMethods(tables));
	}

	public String generateFileName() {
		return super.prop.getProperty("dir")
				+ super.prop.getProperty("package").replace(".", "\\")
				+ "\\dao\\factory\\" + "DaoFactory.java";
	}
	
	/**
	 * 根据所有的Table，生成Factory中的getDao方法
	 * @param tables
	 * @return	GET_METHODS
	 */
	public String generateDaoGetMethods(List<Table> tables) {
		String getMethods = "";
		for (int i = 0; i < tables.size(); i++) {
			Table t = tables.get(i);
			getMethods += GET_METHODS;
			getMethods = 
					getMethods.
					replace( "CLASS_NAME", StringUtils.generateClassName(t.getName())).
					replace( "CLASS_PARAMETER", StringUtils.generateParameterName(t.getName()));
		}
		return getMethods;
	}

	public static void main(String[] args) {
		MetaData meta = MetaData.getInstance();
		List<Table> tables = meta.getTables();
		FactoryGenerator g = new FactoryGenerator();
		System.out.println(g.generate(tables));
	}

}
